import {inject, observer} from "mobx-react";
import React from "react";
import * as asyncLoad from '../util/asyncLoad';
import {workflowPublicFiles} from "../constants/fileConfig";
import Loading from "./Loading";
import base from "../util/base";
import * as authTypes from "../constants/secondAuthType";
import {verifyRemark} from "../util/signUtil";

export default class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            hasLoad: false,
            store: null
        };
    }

    componentDidMount() {
        const init = () => {
            const {SecondAuthStore} = window.weaWorkflowPublic;     //公共组件的store，与本组件的secondAuthStore不同，请注意
            this.setState({hasLoad: true, store: SecondAuthStore});
        }
        asyncLoad.asyncLoadModule('workflowPublic', workflowPublicFiles, init);
    }

    //验证成功后的回调方法
    onAuthSuccess = (result) => {
        const {globalStore, secondAuthStore} = base;
        // globalStore.changeSubmitParam({isAuthSuccess: 1});  //二次验证成功
        globalStore.controlDialogVisible(authTypes.SECOND_AUTH_DIALOG, false);     //关闭窗口
        const {callbackFunc, callbackParam} = secondAuthStore;

        const {isProtectDialog, secondAuthType} = result;

        if (isProtectDialog == 1) {     //数据保护：CA或者契约锁数据保护
            secondAuthStore.setDialogResult({isProtectSuccess: 1});  //数据保护成功
        } else if (isProtectDialog == 10) {     //CA认证 + CA数据保护 或者 契约锁认证 + 契约锁数据保护
            secondAuthStore.setDialogResult({isProtectSuccess: 1});  //数据保护成功
            secondAuthStore.setDialogResult({isAuthSuccess: 1});  //二次验证成功
        } else {        //二次验证：CA或者契约锁验证
            secondAuthStore.setDialogResult({isAuthSuccess: 1});  //二次验证成功
        }

        
        //契约锁验证，保存印章信息
        if (secondAuthType == authTypes.QYS) {
            const { sealId: qysSealId, sealImg: qysSealImg, certDetails: qysCertDetails, sealSpec = {} } = result; // 契约锁印章，印章id信息,certDetails：印章信息，json字符串
            let { width: qyswidth = 0, height: qysheight = 0 } = sealSpec;
            qyswidth *= 2.8346;
            qysheight *= 2.8346;
            const { params } = secondAuthStore;
            secondAuthStore.setParams({
                ...params,
                qysSealId,
                qysSealImg,
                qysCertDetails: JSON.stringify({ ...qysCertDetails, qyswidth, qysheight })
            });
        }

        if (callbackFunc && typeof callbackFunc == "function") {
            callbackFunc.call({}, callbackParam);    //调用提交流程的方法
        }
    }

    //验证失败后的回调方法
    onAuthFail = () => {
        //nothing to do
    }

    //关闭窗口
    onClose = () => {
        const {globalStore, secondAuthStore} = base;
        globalStore.controlDialogVisible(authTypes.SECOND_AUTH_DIALOG, false);

        //重置与弹出框以及验证结果相关的参数
        secondAuthStore.resetStore();
    }

    render() {
        const {hasLoad, store} = this.state;

        const {globalStore, secondAuthStore} = base;
        const {params, protectType, isProtectDialog} = secondAuthStore;
        let {secondAuthType} = secondAuthStore;
        const {visible} = this.props;

        const {authKey = "", signUrl = "", sourcestr = ""} = params;

        if (isProtectDialog == 1) {
            secondAuthType = protectType;
        }

        if (hasLoad) {
            const {SecondAuthCom, QYSUkey} = window.weaWorkflowPublic;
            store.setAuthData({authKey, secondAuthType, isProtectDialog, signUrl});

            return (
                <SecondAuthCom ecId={`${this && this.props && this.props.ecId || ''}_SecondAuthCom@jqw08m`}
                    visible={visible}
                    secondAuthStore={store}
                    onSuccess={this.onAuthSuccess}
                    onFail={this.onAuthFail}
                    onClose={this.onClose}/>
            )
        } else {
            return (<Loading ecId={`${this && this.props && this.props.ecId || ''}_Loading@d4j862`}/>);
        }
    }
}